﻿function ProcessingWindow(show) {
    var inframe = frameElement ? true : false;
    if (!show) {
        if (window.processingWindow) {
            window.clearTimeout(processingWindow.timeoutChecker);
            DisposeFrame(processingWindow.contentWindow);
            processingWindow.parentNode.removeChild(processingWindow);
            window.processingWindow = null;
            if (window.processingStartDate) {
                if (window.logger) {
                    logger.Log("耗时：" + (new Date() - processingStartDate));
                };
                processingStartDate = null;
            }
        };
        return;
    };
    if (!window.processingWindow) {
        window.processingWindow = document.createElement("iframe");
        processingWindow.frameBorder = 0;
        processingWindow.scrolling = "no";
        with (processingWindow.style) {
            position = "absolute";
            width = processing_window_width;
            height = processing_window_height;
        };
        document.body.appendChild(processingWindow);

        var wdoc = null;
        if (window.attachEvent) {
            wdoc = processingWindow.contentWindow.document;
        } else if (window.addEventListener) {
            wdoc = processingWindow.contentDocument;
        }

        var main = wdoc.createElement("div");
        with (main.style) {
            width = processing_window_width;
            height = processing_window_height;
            margin = 0;
            border = processing_window_border;
            padding = 2;
            backgroundColor = processing_window_bgcolor;
            textAlign = "center";
        };

        if (window.attachEvent) {
            wdoc.appendChild(main);
        } else if (window.addEventListener) {
            wdoc.body.appendChild(main);
        }

        wdoc.onselectstart = function () {
            return false;
        };
        var info = wdoc.createElement("div");
        with (info.style) {
            padding = 2;
            fontSize = 12;
            fontWeight = 300;
            color = processing_window_fgcolor;
        };
        info.innerText = processing_window_tip;
        main.appendChild(info);

        var processBar = wdoc.createElement("img");
        with (processBar) {
            style.marginLeft = 1;
            border = 0;
            style.width = processing_window_width - 10;
            style.height = 18;
            src = processingBarImageUrl;
        };

        main.appendChild(processBar);

        processingWindow.timeoutChecker = window.setTimeout(function () {
            info.innerText = processing_window_timeout_tip;
            info.style.color = processing_window_timeout_clr;
            info.style.cursor = "pointer";
            processBar.style.display = "none";
            info.onclick = function () {
                info.onclick = null;
                ProcessingWindow(false);
            };
        }, processing_window_timeout);
    } else {
        processingWindow.style.display = "";
    };
    var width = document.body.clientWidth;
    var height = document.body.clientHeight;
    if (inframe) {
        width = frameElement.clientWidth;
        height = frameElement.clientHeight;
    };
    var x = width / 2 - processing_window_width / 2 + 10;
    var y = height / 2 - processing_window_height;

    processingWindow.style.left = x > 0 ? x + "px" : 0 + "px";
    processingWindow.style.top = y > 0 ? y + "px" : 0 + "px";

    window.processingStartDate = new Date();
};

function DisposeFrame(frameWindow) {
    if (frameWindow.frames && frameWindow.frames.length > 0) {
        for (var i = 0; i < frameWindow.frames.length; i++) {
            DisposeFrame(frameWindow.frames[i]);
        }
    };
    var nodes = frameWindow.document.childNodes;
    for (var i = 0; i < nodes.length; i++) {
        DisposeNode(nodes[i]);
    };
    DisposeNode(frameWindow.frameElement);
    frameWindow.frameElement.src = "javascript: false;";
};

function DisposeNode(node) {
    var attrs = node.attributes;
    if (attrs && attrs.length > 0) {
        var name = null;
        for (var i = 0; i < attrs.length; i++) {
            name = attrs[i];
            if (typeof node[name] == "function") {
                node[name] = null;
            }
        }
    };
    if (node.childNodes) {
        for (var i = 0; i < node.childNodes.length; i++) {
            DisposeNode(node.childNodes[i]);
        }
    }
};